﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using IHISCETeim.Library;
using IHISCETeim.Model;
using IHISCETeim.DAL;
using System.Data;
using System.Threading;

namespace IHISCETeim.BLL
{
    public class SBOController:BaseController
    {
        /// <summary>
        /// 根据序列号获取相关的原始BOM
        /// </summary>
        /// <param name="IntrSerial">序列号</param>
        /// <returns></returns>
        public static DataTable GetBOMByIntrSerial(string IntrSerial,string sbodb)
        {
            return SBODAL.GetBOMByIntrSerial(IntrSerial, sbodb);
        }
      
        /// <summary>
        /// 获取物料最后一次采购价(实时)
        /// </summary>
        /// <param name="ItemCode"></param>
        /// <returns>返回格式为  RMB</returns>
        public static string GetOITMLastPurPrice(string ItemCode)
        {
            return SBODAL.GetOITMLastPurPrice(ItemCode);
        }
        /// <summary>
        /// 获取经销商销售价
        /// </summary>
        /// <param name="ItemCode"></param>
        /// <returns></returns>
        public static string GetOITMAgentPrice(string ItemCode)
        {
            return SBODAL.GetOITMAgentPrice(ItemCode);
        }

        public static string GetCardInfo(string cardcode)
        {
            return SBODAL.GetCardInfo(cardcode);
        }
        /// <summary>
        /// 获取所有的代理商列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAgentList()
        {
            DataTable dt = DC.Get<DataTable>(GetAgentListALLKey);
            if (dt == null)
            {
                dt = SyncSAPDAL.GetAgentList();
                DC.Add(GetAgentListALLKey, dt);
            }
            return dt;
        }
        public static DataTable GetAgentList(string key, int toprow)
        {
            DataTable dt = GetAgentList();
            DataRow[] drs = dt.Select("AliasName like '%" + key + "%' or CardCode like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }
        /// <summary>
        /// 根据全称获取简称
        /// </summary>
        /// <param name="CardName"></param>
        /// <returns></returns>
        public static string GetAliasNameByCardName(string CardName)
        {
            DataTable dt = GetAgentList();
            DataRow[] drs = dt.Select(string.Format(" CardName ='{0}'",CardName.Trim()));
            if(drs.Count()<=0)
                return "";
            else
                return drs[0]["AliasName"].ToString();
        }
        /// <summary>
        /// 获取所有的原材料及配件的物料
        /// </summary>
        /// <returns></returns>
        public static DataTable GetOITMList()
        {
            DataTable dt = DC.Get<DataTable>(GetOITMListALLKey);
            if (dt == null)
            {
                dt = SBODAL.GetOITMList();
                DC.Add(GetOITMListALLKey, dt);
            }
            return dt;
        }
        /// <summary>
        /// 获取物料当前的库位及库存
        /// </summary>
        /// <param name="itemcode"></param>
        /// <param name="whscode"></param>
        /// <returns></returns>
        public static DataTable GetOITMSWWOnHand(string itemcode, string whscode)
        {
            return SBODAL.GetOITMSWWOnHand(itemcode, whscode);
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetOITMList(string key,int toprow)
        {
            DataTable dt = GetOITMList();
            DataRow[] drs = dt.Select(" itemcode like '%" + key + "%' or itemname like '%" + key + "%' ");
            if(drs.Count()<=0)
                return null;
            dt=  drs.Take(toprow).CopyToDataTable();
            return dt;
        }
        /// <summary>
        /// 获取配件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetPaiJinOITMList(string key, int toprow)
        {
            DataTable dt = GetOITMList();
            DataRow[] drs = dt.Select(" itmsGrpCod='102' and (itemcode like '%" + key + "%' or itemname like '%" + key + "%')");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }


        /// <summary>
        /// 获取原材料
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetYanCaiLiaoOITM(string key, int toprow)
        {
            DataTable dt = GetOITMList();
            DataRow[] drs = dt.Select(" itmsGrpCod='101' and (itemcode like '%" + key + "%' or itemname like '%" + key + "%')");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }

        public static DataTable GetSupplierList()
        {
            DataTable dt = DC.Get<DataTable>(GetSupplierListALLKey);
            if (dt == null)
            {
                dt = SBODAL.GetSupplierList();
                DC.Add(GetSupplierListALLKey, dt);
            }
            return dt;
        }
        /// <summary>
        /// 供应商列表查询
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toprow"></param>
        /// <returns></returns>
        public static DataTable GetSupplierList(string key, int toprow)
        {
            DataTable dt = GetSupplierList();
            DataRow[] drs;
            if(string.IsNullOrEmpty(key))
                 drs = dt.Select("");
            else
                drs = dt.Select("AliasName like '%" + key + "%' or CardCode like '%" + key + "%'");
            if (drs.Count() <= 0)
                return null;
            dt = drs.Take(toprow).CopyToDataTable();
            return dt;
        }

        /// <summary>
        /// 获取生产订单对比结果
        /// </summary>
        /// <param name="DocEntry1"></param>
        /// <param name="DocEntry2"></param>
        /// <returns></returns>
        public static DataTableCollection IHISCETeim_OWORComparison(int DocEntry1, int DocEntry2)
        {
            return SBODAL.IHISCETeim_OWORComparison(DocEntry1, DocEntry2);
        }

         /// <summary>
        /// 获取厦门整机库存数据
        /// </summary>
        /// <param name="ItemCode"></param>
        /// <returns></returns>
        public static object Get_W01001_OnHand(string ItemCode)
        {
            return SBODAL.Get_W01001_OnHand(ItemCode);
        }
        /// <summary>
        /// 获取进口整机库存列表
        /// </summary>
        /// <returns></returns>
        public static DataTable GetJKMacList()
        {
            return  SBODAL.GetJKMacList();
        }

        #region 原材料货款支付相关
        /// <summary>
        /// 获取需处理的收货采购订单
        /// </summary>
        /// <param name="sqlwhere">查询条件，不带ＷＨＥＲＥ</param>
        /// <param name="topRow">要获取几行</param>
        /// <returns></returns>
        public static DataTable GetOPDNList(string sqlwhere, int topRow)
        {
            return SBODAL.GetOPDNList(sqlwhere, topRow);
        }

        /// <summary>
        /// 获取收货采购订单－明细
        /// </summary>
        /// <param name="DocEntry">收货采购单的自增长号码，不是单号</param>
        /// <returns></returns>
        public static DataTable GetOPDNDetail(int DocEntry)
        {
            return SBODAL.GetOPDNDetail(DocEntry);
        }
        /// <summary>
        /// 更新支付时间
        /// </summary>
        /// <param name="DocEntry"></param>
        /// <param name="payDate"></param>
        /// <returns></returns>
        public static bool UpdateOPDNPayDate(int DocEntry, string payDate)
        {
            return SBODAL.UpdateOPDNPayDate(DocEntry, payDate);
        }
        #endregion
        /// <summary>
        /// 是否存在相应物料 代码
        /// </summary>
        /// <param name="itemcode"></param>
        /// <returns>存在返回物料名称</returns>
        public static string GetHaveItemCode(string itemcode)
        {
            return SBODAL.GetHaveItemCode(itemcode);
        }
        public static DataTableCollection ihi_yyx_dzb()
        {
            return SBODAL.ihi_yyx_dzb();
        }

        public static DataTable GetQueryParts(string sqlwhere)
        {
            return SBODAL.GetQueryParts(sqlwhere);
        }
    }
}
